package com.jack.payment.service.common;

import com.jack.payment.constant.enums.BillingStatusEnum;
import com.jack.payment.dao.billing.BillingDao;
import com.jack.payment.dao.payment.PaymentAttachDao;
import com.jack.payment.dao.user.UserApiDao;
import com.jack.payment.entity.generate.billing.BillingModel;
import com.jack.payment.entity.generate.payment.PaymentAttachModel;
import com.jack.payment.entity.generate.user.UserApiModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class PaymentSuccessService {
    private Logger log = LoggerFactory.getLogger(PaymentSuccessService.class);

    @Autowired
    private BillingDao billingDao;
    @Autowired
    private PaymentAttachDao paymentAttachDao;
    @Autowired
    private UserApiDao userApiDao;


    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void paymentSuccessBoHandler(PaymentAttachModel paymentAttachModel,
                                        PaymentAttachModel sysAttachModel) {
        paymentAttachDao.updatePayAttachContent(paymentAttachModel);
        userApiDao.updateUserApiKeyInfo(new UserApiModel().builder()
                .apiKey(sysAttachModel.getApiKey())
                .user(sysAttachModel.getUser())
                .paymentCount(1)
                .build());
        billingDao.insertBillingRecord(new BillingModel().builder()
                .status(BillingStatusEnum.PAY.getStat())
                .channel(paymentAttachModel.getChannel())
                .paymentEnv(sysAttachModel.getPaymentEnv())
                .user(sysAttachModel.getUser())
                .channelNo(paymentAttachModel.getChannelNo())
                .paymentAmount(paymentAttachModel.getActualAmount())
                .createTime(paymentAttachModel.getSuccessTime())
                .orderNo(paymentAttachModel.getOrderNo())
                .currency(paymentAttachModel.getCurrency())
                .build());
    }

}
